/**
 * 
 */
package com.zc.BankCreditManagement.ServiceImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.zc.BankCreditManagement.DAO.DepositDetailsDAO;
import com.zc.BankCreditManagement.JavaBean.DepositDetails;
import com.zc.BankCreditManagement.Service.DepositDetailsService;
import com.zc.BankCreditManagement.ZCUtil.GenerateIdUtil;

/**
 * @author ZhangCheng
 * @time 2019年9月18日
 */
public class DepositDetailsServiceImpl implements DepositDetailsService {
	DepositDetailsDAO depDAO;

	public DepositDetailsServiceImpl() {
		this.depDAO = new DepositDetailsDAO();
	}

	/**
	 * 增加新质押物订单
	 */
	@Override
	public int insertNewToDB(DepositDetails dep) {
		return this.depDAO.insertNewDeposit(dep);
	}

	/**
	 * 根据Id查询质押物订单
	 * 
	 * @return 查询不到返回null
	 */

	@Override
	public DepositDetails getThisInfoById(int depositId,boolean excludeDelete) {

		return this.depDAO.getDepositDetailByDepositId(depositId,excludeDelete);
	}

	/**
	 * 根据Id查询质押物订单
	 * 
	 * @return 查询不到返回null
	 */
	@Override
	public int deleteThisToDB(int depositId,boolean excludeDelete) {
		if (this.depDAO.getDepositDetailByDepositId(depositId, excludeDelete) != null) {
			return this.depDAO.deleteDepositById(depositId);
		} else {
			System.out.println("删除失败！您的质押物信息有误，不存在该质押物订单或已经被删除，请检查您的信息！");
		}
		return 0;
	}

	@Override
	public ArrayList<DepositDetails> getAllSuitable(boolean excludeDelete) {
		StringBuffer sql = new StringBuffer("select * from Customer ");
		if (excludeDelete)
			sql.append("isdelete = 1");
		return this.depDAO.getDepositDetailssBySQLFromDB(sql.toString());
	}
	/**
	 * 根据cusId查询正在执行的质押物订单
	 * 
	 * @return 查询不到返回null
	 */
	public ArrayList<DepositDetails> getAllLoaningDepositsById(int cusId){
		return this.depDAO.getDepositDetailsByCusId(cusId, true);
	}
	/**
	 * 根据manId查询正在执行的质押物订单
	 * 
	 * @return 查询不到返回null
	 */
	public ArrayList<DepositDetails> getAllLoaningDepositsByManId(int manId){
		return this.depDAO.getDepositDetailsByManId(manId, true);
	}
	/**
	 * 	根据用户Id和用户输入客户要提交的质押物，质押物数量,预估价格
	 *  @return 字符串  Max "贷款上限达到最大值"
	 *   				null "插入失败"
	 *  				"" + manId "客户所被分配的客户经理"
	 * @throws ParseException 
	 * @throws NumberFormatException 
	 */
	public String cusDepositDetailsByCusId(int cusId,String cusWantLoanDeposit,String cusWantLoanDepositNum,String cusWantLoanDepositPrice) throws NumberFormatException, ParseException {
		ManagerServiceImpl mSI = new ManagerServiceImpl();
	
		int manId = mSI.ranGetOneMan().getManId();
		
		DepositDetails dD = new DepositDetails(GenerateIdUtil.generateCusDepositDetailsId(cusId), cusId, manId, cusWantLoanDeposit, 
				Integer.parseInt(cusWantLoanDepositNum), 
				Long.parseLong(cusWantLoanDepositPrice), 0L, new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date())));
		
		System.out.println(dD.getInfo());
		
		ArrayList<DepositDetails> dDLoaning = getAllLoaningDepositsById(cusId);
		
		if(dDLoaning == null||dDLoaning.size()<7) {
			if(insertNewToDB(dD)==1) {
				return ""+manId;
			}else{
				return "null";
			}
		}else {
			return "Max";
		}
	}
}
